/**
 * Created by Xiaoming on 2017/8/29.
 */
angular.module('module.auth')
    .controller('RegisterController', RegisterController);

RegisterController.$inject = ['$state', 'AuthService', 'AuthUserService'];
function RegisterController($state, AuthService, AuthUserService) {
    var vm = this;
    vm.user = {};
    vm.getcode = getcode;
    vm.register = register;
    //获取验证码
    function getcode(user) {
        if (!judgePhone(user.phone)) {
            $(".tel").focus();
            $(".tel").select();
            return;
        }
        settime();
        AuthService.getcode(user.phone,'register', function () {
        }, function (reason) {
            if(reason.data){
                settime(0);
                input_alert(reason.data.msg);
            }else{
                input_alert("发送短信失败，请重试");
            }
        });
    }
    //注册操作
    function register(user) {
        if (!judgePhone(user.phone)) {
            $(".tel").focus();
            $(".tel").select();
            return;
        }
        if (!judgeNickname(user.nickname)) {
            $(".name").focus();
            $(".name").select();
            return;
        }
        if (!judgePass(user.password)) {
            $(".psw").focus();
            $(".psw").select();
            return;
        }
        if (!user.code || user.code == "") {
            input_alert("请输入验证码");
            return;
        }
        user.name = user.phone;
        AuthService.register(vm.user, function (data) {
            input_alert('注册成功', 1000);
            setTimeout(function () {
                $state.go("botMenu.myview");
            }, 1000);
            AuthUserService.addUserToCookies(data);
        }, function (error) {
            if(error.data.msg){
                input_alert("注册失败！" + error.data.msg, 3000);
            }
            if(error.data.reason){
                input_alert("注册失败！" + error.data.reason, 3000);
            }
        });
    }
}